
# include "dfxMemoryFile.h"
# include "dfxLog.h"

namespace DFX
{
    void MemoryFile::Create(UInt64 size)
    {
        mBuffer = new UInt8[static_cast<UInt32>(size)];
        mSize = size;
        mOwnsBuffer = true;
    }

    void MemoryFile::CreateFromString(const String &str)
    {
        mSize = str.length();

        if (mSize > 0)
        {
            Int8 *localBuffer = new Int8[static_cast<UInt32>(mSize)];
            memcpy(localBuffer, str.c_str(), static_cast<UInt32>(mSize));

            mBuffer = localBuffer;
            mOwnsBuffer = true;
        }
    }

    void MemoryFile::CreateFromBuffer(const void *buffer, UInt64 size)
    {
        mBuffer = buffer;
        mSize = size;
        mOwnsBuffer = true;
    }

    void MemoryFile::CreateFromManagedBuffer(const void *buffer, UInt64 size)
    {
        mBuffer = buffer;
        mSize = size;
        mOwnsBuffer = false;
    }

    void MemoryFile::DeleteBuffer()
    {
        if (mSize > 0)
        {
            if (mOwnsBuffer)
                delete[] reinterpret_cast<const char*>(mBuffer);
        }

        mBuffer = NULL;
        mSize = 0;
    }
}